home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2687 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Malloc failing with Huge Arrays under VC++ 1.0
  5. Date: 23 Jan 1996 06:36:41 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4e1vlp$cc1@news.iag.net>
  8. References: <pXsAxAWLBh107h@perception.co.nz>
  9. NNTP-Posting-Host: pm1-orl13.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <pXsAxAWLBh107h@perception.co.nz>, simond@perception.co.nz says...
  13. >
  14. >Hi All,
  15. >
  16. >ok, panic time here, trying to get huge arrays working under VC++, code works
  17. >something like this (2 major arrays):
  18. >
  19. >------------------------------------------ 
  20. >
  21. >  //declare position as a huge global array
  22. >  unsigned long __huge *position;                                            
  23. >
  24. >
  25. >  void main(void)
  26.  
  27. Ouch!! This is a definite no no in c.l.c!  main returns int in ansi c, which
  28. is the purpose of this group.  You might want to d/l and read through the
  29. c.l.c faq (Frequently Asked Question) list.  It is available for anonymous
  30. ftp from rtfm.mit.edu /pub/usenet/comp.lang.c (the faqs for most groups can
  31. be found there).  I suspect that you would find the answer to your problem
  32. in it as well (directly or indirectly).
  33.  
  34. >  {
  35. >  
  36. >  [snip other minor (nothing big) declarations]
  37. >
  38. >
  39. >  //set the maximum numbers we can deal with
  40. >  unsigned long MAX_ARRAY_SIZE = 16383;
  41. >
  42. >  //malloc the position array - yes, unsigned long is same size as regular 
  43. long!
  44. >  if (!(position = (unsigned long __huge *)malloc(MAX_ARRAY_SIZE * 
  45. sizeof(unsigned long))) ) return;
  46.  
  47. The definition fo the ansi function malloc is:
  48.  
  49.   void *malloc( size_t size)
  50.  
  51. I suspect that you'll find that on your compiler sizeof(size_t) == 2 (it is
  52. probably defined as unsigned int.  Look in stdlib.h).  This means that the
  53. largest value it can possibly hold is (assuming CHAR_BIT == 8 in limits.h)
  54. 65535.  You'll probably also find that sizeof(float) == 4.  So the largest
  55. MAX_ARRAY_SIZE would be about 65535 / 4 or 16383 floats.
  56.  
  57. Look into the system/compiler specific halloc function.  It should allow
  58. you to allocate larger blocks.  Because this is a non-ansi function, you 
  59. should really direct any questions that pertain specifically to it to a 
  60. group like 
  61.  
  62. comp.os.msdos.programmer
  63. comp.os.ms-windows.programmer[.tools.mfc]
  64.  
  65.  
  66. BTW, since malloc returns a void pointer, the cast on the return is not
  67. necessary in ansi c (probably is in c++, though).  The use of unnecessary
  68. casts is a bit risky, because it can hide errors that the compiler would 
  69. normally point out to you. You should probably avoid using casts, except
  70. when you have a specific need for one and fully understand the possible 
  71. side effects.
  72.  
  73. -- 
  74. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  75. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  76.  
  77.